<template>
    <el-dialog
        title="角色信息"
        :visible.sync="show"
        :before-close="handleCancel"
        :close-on-click-modal="false"
        width="600px"
    >
        <div v-loading="loadingGet" v-if="reset">
            <el-form :model="formData" :rules="rules" ref="form">
                <el-form-item label="角色名：" prop="name" label-width="140px">
                    <el-input v-model="formData.name" auto-complete="off" placeholder="请输入角色名"></el-input>
                </el-form-item>
                <el-form-item label="角色唯一编码：" prop="code" label-width="140px">
                    <el-input v-model="formData.code" auto-complete="off" placeholder="请输入角色唯一编码"></el-input>
                </el-form-item>
                <el-form-item label="描述：" prop="description" label-width="140px">
                    <el-input type="textarea" v-model="formData.description" auto-complete="off" placeholder="请输入描述"></el-input>
                </el-form-item>
            </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button @click="handleCancel" size="small">取消</el-button>
            <el-button type="primary" @click="handleSubmit" size="small" :loading="loadingSubmit">确定</el-button>
        </div>
    </el-dialog>
</template>

<script>
import role_api from '../../api/role_api'
export default {
    props: {
        id: Number,
        show: Boolean,
        success: Function,
        cancel: Function,
    },
    watch: {
        show(val) {
            if (val) {
                this.reset = false
                this.$nextTick(() => {
                    Object.assign(this.$data, this.$options.data.call(this))
                    this.reset = true
                    this.init && this.init()
                })
            }
        }
    },
    data() {
        return {
            reset: true,
            formData: {
            },
            rules: {
                name: [
                    { required: true, message: '请输入角色名', trigger: 'blur' }
                ],
                code: [
                    { required: true, message: '请输入角色唯一编码', trigger: 'blur' }
                ],
                description: [
                    {
                        type: 'string',
                        max: 100,
                        message: '不要超过100个字符',
                        trigger: 'blur'
                    }
                ]
            },
            loadingSubmit: false,
            loadingGet: false,
        }
    },
    methods: {
        init() {
            this.loadingGet = true
            role_api.get(this.id).then(data => {
                this.loadingGet = false
                this.formData = data
            }).catch(err => {
                this.loadingGet = false
                this.$message.error(err.message)
            })
        },
        handleSubmit(callback) {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.loadingSubmit = true
                    role_api.updateRole(this.formData).then(data => {
                        this.loadingSubmit = false
                        this.$message.success('修改成功!')
                        this.success()
                    }).catch(err => {
                        this.loadingSubmit = false
                        this.$message.error(err.message)
                    })
                }
            })
        },
        handleCancel() {
            this.cancel()
        },
    }
}
</script>

<style>
</style>